package com.wsg.bindsysapp;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import com.jaredrummler.android.processes.ProcessManager;
import com.jaredrummler.android.processes.models.AndroidAppProcess;

import java.util.Collections;
import java.util.List;

public class GetTaskService extends Service {
    public GetTaskService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("info", "onCreate..................");

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    SystemClock.sleep(2000);
                    getTask();
                }
            }
        }).start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("info", "onStartCommand..................");
        return super.onStartCommand(intent, flags, startId);
    }

    public void getTask() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //For versions less than lollipop
            Log.e("info", "小于  21...........................");
            ActivityManager am = ((ActivityManager) getSystemService(ACTIVITY_SERVICE));
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(5);
            String top = taskInfo.get(0).topActivity.getPackageName();
            Log.e("info", "top app = " + top);
        } else { //For versions Lollipop and above
            Log.e("info", "大于  21...........................");
            List<AndroidAppProcess> processes = ProcessManager.getRunningForegroundApps(getApplicationContext());

            Collections.sort(processes, new ProcessManager.ProcessComparator());
            String tasks = "";
            String top = "";
            for (int i = 0; i <= processes.size() - 1; i++) {
                tasks += processes.get(i).name + "\n";
                if (processes.get(i).name.equalsIgnoreCase("com.google.android.gms")) { //always the package name above/below this package is the top app
                    if ((i + 1) <= processes.size() - 1) { //If processes.get(i+1) available, then that app is the top app
                        top = processes.get(i + 1).name;
                    } else if (i != 0) { //If the last package name is "com.google.android.gms" then the package name above this is the top app
                        top = processes.get(i - 1).name;
                    } else {
                        if (i == processes.size() - 1) { //If only one package name available
                            top = processes.get(i).name;
                        }
                    }
                    Log.e("info", "top app = " + top);
                }
            }
            Toast.makeText(this, tasks, Toast.LENGTH_SHORT).show();
        }
    }


}
